package com.xp.flower.qsd.share.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xp.flower.qsd.share.token.AccessToken;
import com.xp.flower.qsd.share.token.JsApiTicket;
import com.xp.flower.qsd.share.token.Oauth2Toekn;

/**
 * 
 * 获取微信token信息，token的有效期为2小时，每二小时要重新获取一次
 * 获取微信token信息每天限2000次
 * @author yyp
 *
 */
public class TokenHandler 
{
	private static AccessToken accessToken;
	private static AccessToken qyAccessToken;
	private static JsApiTicket jsApiTicket;
	private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	private static final String OAUTH2_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";
	private static final String JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
	private static final String QY_ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CORPID&corpsecret=CORPSECRET";		//企业号获取accessToken的链接
	
	/**
	 * 获取token，token有效期二小时
	 * @param appid
	 * @param appsecret
	 * @return null时代表失败
	 */
	public static String getAccessToken(String appid, String appsecret)
	{
		if (accessToken != null && accessToken.getLastTime() > 0)
		{
			if ((System.currentTimeMillis() - accessToken.getLastTime()) < (2 * 60 * 60 * 1000L))
			{
				return accessToken.getToken();
			}
		}
		
		String requestUrl = ACCESS_TOKEN_URL.replace("APPID", appid).replace("APPSECRET", appsecret); 
		JSONObject jsonObject = HttpUtils.httpRequest(requestUrl, HttpUtils.GET, null);
		
		if (jsonObject != null)
		{
			if (jsonObject.getIntValue("errcode") == 0)					// 等于0代表没有出错
			{
				accessToken = new AccessToken();
				accessToken.setToken(jsonObject.getString("access_token"));
				accessToken.setExpiresIn(jsonObject.getIntValue("expires_in"));
				accessToken.setLastTime(System.currentTimeMillis());
			}
			else
			{
				System.out.println("getAccessToken err:" + jsonObject.toJSONString());
			}
		}
		
		return accessToken.getToken();
	}
	
	/**
	 * 获取企业号token，token有效期二小时
	 * @param appid
	 * @param appsecret
	 * @return null时代表失败
	 */
	public static String getQYAccessToken(String corpid, String corpsecret)
	{
		if (qyAccessToken != null && qyAccessToken.getLastTime() > 0)
		{
			if ((System.currentTimeMillis() - qyAccessToken.getLastTime()) < (2 * 60 * 60 * 1000L))
			{
				return qyAccessToken.getToken();
			}
		}
		
		String requestUrl = QY_ACCESS_TOKEN_URL.replace("CORPID", corpid).replace("CORPSECRET", corpsecret); 
		JSONObject jsonObject = HttpUtils.httpRequest(requestUrl, HttpUtils.GET, null);
		
		if (jsonObject != null)
		{
			if (jsonObject.getIntValue("errcode") == 0)					// 等于0代表没有出错
			{
				qyAccessToken = new AccessToken();
				qyAccessToken.setToken(jsonObject.getString("access_token"));
				qyAccessToken.setExpiresIn(jsonObject.getIntValue("expires_in"));
				qyAccessToken.setLastTime(System.currentTimeMillis());
			}
			else
			{
				System.out.println("getQYAccessToken err:" + jsonObject.toJSONString());
			}
		}
		
		return qyAccessToken.getToken();
	}
	
	/**
	 * 获取网页授权凭证
	 * @param appid			
	 * @param appsecret
	 * @param code
	 * @return null时代表失败
	 */
	public static Oauth2Toekn getOauth2AccessToken(String appid, String appsecret, String code)
	{
		Oauth2Toekn at = null;
		
		String requestUrl = OAUTH2_TOKEN_URL.replace("APPID", appid).replace("APPSECRET", appsecret).replace("CODE", code);
		JSONObject jsonObject = HttpUtils.httpRequest(requestUrl, HttpUtils.GET, null);
		
		if (jsonObject != null)
		{
			if (jsonObject.getIntValue("errcode") == 0)					// 等于0代表没有出错
			{			
				at = JSON.parseObject(jsonObject.toJSONString(), Oauth2Toekn.class);
			}
			else
			{
				System.out.println("getOauth2AccessToken err:" + jsonObject.toJSONString());
			}
		}
		
		return at;
	}
	
	/**
	 * 获取token，token有效期二小时
	 * @param appid
	 * @param appsecret
	 * @return null时代表失败
	 */
	public static String getJsApiTicket(String appid, String appsecret)
	{
		if (jsApiTicket != null && jsApiTicket.getLastTime() > 0)
		{
			if ((System.currentTimeMillis() - jsApiTicket.getLastTime()) < (2 * 60 * 60 * 1000L))
			{
				return jsApiTicket.getTicket();
			}
		}
		
		String requestUrl = JSAPI_TICKET_URL.replace("ACCESS_TOKEN", getAccessToken(appid, appsecret));
		JSONObject jsonObject = HttpUtils.httpRequest(requestUrl, HttpUtils.GET, null);
		
		if (jsonObject != null)
		{
			if (jsonObject.getIntValue("errcode") == 0)					// 等于0代表没有出错
			{
				jsApiTicket = new JsApiTicket(jsonObject.getString("ticket"), jsonObject.getIntValue("expires_in"), System.currentTimeMillis());
			}
			else
			{
				System.out.println("getAccessToken err:" + jsonObject.toJSONString());
			}
		}
		
		return jsApiTicket.getTicket();
	}
}